package plus.chendd.jdk8;

import org.junit.Test;
import plus.chendd.service.Operator;

import java.util.Date;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class FunctionInterface {
  
  public static void sumTotal(Operator operator) {
    int[] arr = {1, 2, 3, 4};
    int res = operator.sum(arr);
    System.out.println("res = " + res);
  }
  
  @Test
  public void startTest001() {
    sumTotal((arr) -> {
      int count = 0;
      for (int i : arr) {
        count += i;
      }
      return count;
    });
  }
  
  public static void testSupplier(Supplier<Integer> supplier) {
    Integer integer = supplier.get();
    System.out.println("integer = " + integer);
  }
  
  @Test
  public void startTest002() {
    testSupplier(() -> 10);
  }
  
  public static void testConsumer(Consumer<String> consumer) {
    consumer.accept("hello");
  }
  
  @Test
  public void startTest003() {
    testConsumer((str) -> System.out.println(str));
  }
  
  public static void computedSum(int[] arr) {
    int sum = 0;
    for (int i : arr) {
      sum += i;
    }
    System.out.println("sum = " + sum);
  }
  
  public void total004(Consumer<int[]> consumer) {
    consumer.accept(new int[]{1, 2, 3, 4});
  }
  
  @Test
  public void startTest004() {
    total004(FunctionInterface::computedSum);
  }
  
  @Test
  public void startTest005() {
    Date date = new Date();
    Supplier<Long> supplier = () -> date.getTime();
    System.out.println("supplier.get() = " + supplier.get());
    
    Supplier<Long> supplier1 = date::getTime;
    System.out.println("supplier1 = " + supplier1.get());
  }
  
  public boolean isType(Predicate<String> predicate) {
    return predicate.test("hello");
  }
  
  @Test
  public void startTest006() {
  
  }
}
